package com.ceba.test.modules.admin.service;

import java.util.List;
import java.util.Map;

import com.ceba.base.web.service.IBaseService;
import org.json.JSONObject;

import com.ceba.test.base.language.ResourceUtils;
import com.ceba.test.modules.admin.dao.DirManagerDao;
import com.ceba.test.modules.admin.entity.DirManager;

import abc.data.DataTable;

public class DirManagerService extends IBaseService {

	private DirManagerDao dirManagerDao;

	public DataTable getDataTableById(String Id) throws Exception {
		return dirManagerDao.getDataTableById(Id);
	}

	public DataTable getDataTableByDir(String dirPath) throws Exception {

		return dirManagerDao.getDataTableByDir(dirPath);

	}

	public DataTable getDataTable() throws Exception {

		return dirManagerDao.getDataTable();

	}

	public DirManager getDirById(String ID) throws Exception {

		return dirManagerDao.getDirById(ID);
	}

	public DirManager getDirByDirPath(String dirPath) throws Exception {

		return dirManagerDao.getDirByDirPath(dirPath);
	}

	public List<DirManager> getDirManagers() throws Exception {
		return dirManagerDao.getDirManagers();
	}

	/**
	 * @return
	 */
	public JSONObject getDirList(String orderBy, String key, Map<String, Object> dic) throws Exception {
		return dirManagerDao.getDirList(orderBy, key, dic);
	}

	/**
	 * 增加
	 * 
	 * @param dir
	 * @return
	 */
	public boolean addDir(DirManager dir) {
		return dirManagerDao.addDir(dir);
	}

	/**
	 * 更新
	 * 
	 * @param dir
	 * @return
	 */
	public boolean updateDir(DirManager dir) {
		return dirManagerDao.updateDir(dir);
	}

	/*
	 * 获取错误信息
	 */
	public String getErrorMessage(String newPath, String type, String oldPath, int pathType) throws Exception {
		String err = "";
		List<DirManager> list = getDirManagers();
		if (list != null && list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				DirManager dm = list.get(i);
				if (pathType == 1) {
					if ("create".equals(type)) {
						if (newPath.equals(dm.getDirPath())) {
							err = ResourceUtils.getMessage("MODULES_SAME_ORIGINAL_PATH_EXISTS_LIST");
							break;
						}
					} else if ("modify".equals(type)) {
						if (newPath.equals(dm.getDirPath()) && !oldPath.equals(dm.getDirPath())) {
							err = ResourceUtils.getMessage("MODULES_SAME_ORIGINAL_PATH_EXISTS_LIST");
							break;
						}
					}
					if (newPath.equals(dm.getTempDirPath())) {
						err = ResourceUtils.getMessage("MODULES_CACHE_PATH_SAME_ORIGINAL_PATH_LIGT");
						break;
					}
					if (newPath.equals(dm.getSaveDirPath())) {
						err = ResourceUtils.getMessage("MODULES_STORAGE_PATH_SAME_ORIGINAL_PATH_LIST");
						break;
					}
					if (newPath.equals(dm.getFailDirPath())) {
						err = ResourceUtils.getMessage("MODULES_FAILURE_PATH_SAME_ORIGINAL_PATH");
						break;
					}
				}
				if (pathType == 2) {
					if (newPath.equals(dm.getDirPath())) {
						err = ResourceUtils.getMessage("MOSULES_ORIGINAL_PATH_SAME_CACHE_PATH");
						break;
					}
					if (newPath.equals(dm.getSaveDirPath())) {
						err = ResourceUtils.getMessage("MODULES_STORAGE_PATH_SAME_CACHE_PATH");
						break;
					}
					if (newPath.equals(dm.getFailDirPath())) {
						err = ResourceUtils.getMessage("MODULES_FAILURE_PATH_SAME_CACHE_PATH");
						break;
					}
				}
				if (pathType == 3) {
					if (newPath.equals(dm.getDirPath())) {
						err = ResourceUtils.getMessage("MODULES_ORIGINAL_PATH_SAME_THAT_PATH");
						break;
					}
					if (newPath.equals(dm.getTempDirPath())) {
						err = ResourceUtils.getMessage("MODULES_CACHE_PATH_SAME_STORING_PATH");
						break;
					}
					if (newPath.equals(dm.getFailDirPath())) {
						err = ResourceUtils.getMessage("MODULES_FAILURE_PATH_SAME_STORING_PATH");
						break;
					}
				}
				if (pathType == 4) {
					if (newPath.equals(dm.getDirPath())) {
						err = ResourceUtils.getMessage("MODULES_ORIGINAL_PATH_SAME_FAILURE_PATH");
						break;
					}
					if (newPath.equals(dm.getTempDirPath())) {
						err = ResourceUtils.getMessage("MODULES_CACHE_PATH_SAME_FAILURE_PATH");
						break;
					}
					if (newPath.equals(dm.getSaveDirPath())) {
						err = ResourceUtils.getMessage("MODULES_STORAGE_PATH_SAME_FAILURE_PATH");
						break;
					}
				}
			}
		}
		return err;
	}

	public DirManagerDao getDirManagerDao() {
		return dirManagerDao;
	}

	public void setDirManagerDao(DirManagerDao dirManagerDao) {
		this.dirManagerDao = dirManagerDao;
	}

}
